﻿$(document).ready(function(){
	var bigzindex = $(".handmemo").size();
	var zindex = bigzindex;
	var oldzindex;
	var oldmemo;
	
	function bindnewmemo(handmemo){
		handmemo.mouseover(function(){
			oldzindex = $(this).css("z-index");
			$(this).css({"z-index":bigzindex+1});
			$(this).children(".bar").children(".addspan,.deletespan").show();
		});
		handmemo.mouseout(function(){
			$(this).css({"z-index":oldzindex});
			$(this).children(".bar").children(".addspan,.deletespan").hide();
		});
		handmemo.children(".bar").children(".addspan,.deletespan").mouseover(function(){
			$(this).css({"border-color":"lightblue"});
		});
		handmemo.children(".bar").children(".addspan,.deletespan").mouseout(function(){
			$(this).css({"border-color":"white"});
		});
		handmemo.children(".bar").children(".addspan").click(function(){
			$("#info").html("Adding...");
			var currenthandmemo = $(this).parent().parent();
			$.post("/add",{"memo":""},function(data){
				var newhandmemo = $(data).find("ul .handmemo").first();
				newhandmemo.prependTo("#memos ul");
				newhandmemo.draggable({containment:"#wrapper",scroll:false});
				bigzindex++;
				newhandmemo.css({"top":(currenthandmemo.offset().top-30)+"px","left":(currenthandmemo.offset().left-10)+"px","z-index":bigzindex-1});
				bindhandmemo(newhandmemo);
				newhandmemo.mouseover();
				$("#info").html("");
			});
		});
		handmemo.children(".bar").children(".deletespan").click(function(){
			$("#info").html("Deleting...");
			var currenthandmemo = $(this).parent().parent();
			currenthandmemo.children("#addform").children(".memo").val("");
			currenthandmemo.hide();
			$("#info").html("");
			if($(".handmemo").size()<1){
				$("#addmemo").show();
			}
		});
		handmemo.children("#addform").children(".memo").mouseover(function(){
			$(this).focus();
			oldmemo = $(this).val();
		});
		handmemo.children("#addform").children(".memo").mouseout(function(){
			$(this).blur();
			if($(this).val()!=oldmemo){
				$("#info").html("Updating...");
				var currenthandmemo = $(this).parent().parent();
				$.post("/add",{"memo":$(this).val()},function(data){
					var newhandmemo = $(data).find("ul .handmemo").first();
					newhandmemo.prependTo("#memos ul");
					newhandmemo.draggable({containment:"#wrapper",scroll:false});
					bigzindex++;
					newhandmemo.css({"top":currenthandmemo.offset().top+"px","left":currenthandmemo.offset().left+"px","z-index":bigzindex-1});
					bindhandmemo(newhandmemo);
					newhandmemo.mouseover();
					currenthandmemo.children("#addform").children(".memo").val("");
					currenthandmemo.hide();
					$("#info").html("");
				});
			}
		});
	}
	
	function bindhandmemo(handmemo){
		handmemo.mouseover(function(){
			oldzindex = $(this).css("z-index");
			$(this).css({"z-index":bigzindex});
			$(this).children(".bar").children(".addspan,.deletespan").show();
		});
		handmemo.mouseout(function(){
			$(this).css({"z-index":oldzindex});
			$(this).children(".bar").children(".addspan,.deletespan").hide();
		});
		handmemo.children(".bar").children(".addspan,.deletespan").mouseover(function(){
			$(this).css({"border-color":"lightblue"});
		});
		handmemo.children(".bar").children(".addspan,.deletespan").mouseout(function(){
			$(this).css({"border-color":"white"});
		});
		handmemo.children(".bar").children(".addspan").click(function(){
			$("#info").html("Adding...");
			var currenthandmemo = $(this).parent().parent();
			$.post("/add",{"memo":""},function(data){
				var newhandmemo = $(data).find("ul .handmemo").first();
				newhandmemo.prependTo("#memos ul");
				newhandmemo.draggable({containment:"#wrapper",scroll:false});
				bigzindex++;
				newhandmemo.css({"top":(currenthandmemo.offset().top-30)+"px","left":(currenthandmemo.offset().left-10)+"px","z-index":bigzindex-1});
				bindhandmemo(newhandmemo);
				newhandmemo.mouseover();
				$("#info").html("");
			});
		});
		handmemo.children(".bar").children(".deletespan").click(function(){
			$("#info").html("Deleting...");
			var currenthandmemo = $(this).parent().parent();
			$.post("/delete",{"key":currenthandmemo.children(".deleteform").children(".key").val()},function(){
				currenthandmemo.remove();
				$("#info").html("");
				if($(".handmemo").size()<1){
					$("#addmemo").show();
				}
			});
		});
		handmemo.children(".updateform").children(".memo").mouseover(function(){
			$(this).focus();
			oldmemo = $(this).val();
		});
		handmemo.children(".updateform").children(".memo").mouseout(function(){
			$(this).blur();
			if($(this).val()!=oldmemo){
				$("#info").html("Updating...");
				var currenthandmemo = $(this).parent().parent();
				$.post("/update",{"key": $(this).parent().children(".key").val(), "memo": $(this).val()},function(data){
					var newhandmemo = $(data).find("ul .handmemo").first();
					newhandmemo.prependTo("#memos ul");
					newhandmemo.draggable({containment:"#wrapper",scroll:false});
					bigzindex++;
					newhandmemo.css({"top":currenthandmemo.offset().top+"px","left":currenthandmemo.offset().left+"px","z-index":bigzindex-1});
					bindhandmemo(newhandmemo);
					newhandmemo.mouseover();
					currenthandmemo.remove();
					$("#info").html("");
				});
			}
		});
	}

	$("#addmemo").draggable({containment:"#wrapper",scroll:false});
	$("#addmemo").css({"z-index":bigzindex});
	bindnewmemo($("#addmemo"));
	
	$(".handmemo").each(function(){
		$(this).draggable({containment:"#wrapper",scroll:false});
		zindex--;
		$(this).css({"top":(parseInt(Math.random()*(parseInt($(window).height())-240))+120)+"px","left":(parseInt(Math.random()*(parseInt($(window).width())-200))+100)+"px","z-index":zindex});
		bindhandmemo($(this));
	});
	
	$("#more a").click(function(){
		$(this).blur();
		$.get($(this).attr("href"),function(data){
			var nextpage = $(data).find("#memos").first();
			var handmemos = nextpage.find("ul .handmemo");
			var morelink = nextpage.find("#more a");
			handmemos.appendTo("#memos ul");
			bigzindex += handmemos.size();
			zindex = bigzindex;
			handmemos.each(function(){
				$(this).draggable({containment:"#wrapper",scroll:false});
				zindex--;
				$(this).css({"top":(parseInt(Math.random()*(parseInt($(window).height())-240))+120)+"px","left":(parseInt(Math.random()*(parseInt($(window).width())-200))+100)+"px","z-index":zindex});
				bindhandmemo($(this));
			});
			if(morelink.size() < 1){
				$("#more").remove();
			}
			else{
				$("#more a").attr("href",morelink.first().attr("href"));
			}
		});
		return false;
	});
	
	if($(".handmemo").size()<1){
		$("#addmemo").show();
	}
});